package cn.nbt.busManage.controller;

import cn.nbt.busManage.common.ResponseResult;
import cn.nbt.busManage.model.dto.PointDto;
import cn.nbt.busManage.model.dto.RouteDto;
import cn.nbt.busManage.model.vo.RouteVo;
import cn.nbt.busManage.service.PointService;
import cn.nbt.busManage.service.RouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * {@code @Author} lyq
 * {@code @Date} 6/13/2024 7:23 PM
 */

@RestController
@RequestMapping("/route")
public class RouteController {
    @Autowired
    private RouteService routeService;

    /**
     * 查询所有线路
     * @return 线路信息
     */
    @GetMapping("/list")
    public ResponseResult<Object> findAllRoute() {
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            List<RouteDto> allRoute = routeService.findAllRoute();
            ArrayList<RouteVo> routeVos = new ArrayList<>();
            for (RouteDto routeDto : allRoute) {
                RouteVo routeVo = new RouteVo(routeDto);
                routeVos.add(routeVo);
            }
            result.Success("查询成功",routeVos);
        }catch (Exception e){
            result.Error("查询失败",e.getMessage());
            return result;
        }
        return result;
    }

    /**
     * 根据站点名称查询线路
     * @param pointName 站点名称
     * @return 线路信息
     */
    @GetMapping("/findRouteByPointName")
    public ResponseResult<Object> findRouteByPointName(@RequestParam(name = "pointName") String pointName) {
        ResponseResult<Object> result = new ResponseResult<>();
        try {
            List<RouteDto> routeDtos = routeService.findRouteByPointName(pointName);
            if(routeDtos == null) {
                result.Error("站点不存在或没有经过该站点的路线",null);
                return result;
            }
            ArrayList<RouteVo> routeVos = new ArrayList<>();
            for (RouteDto routeDto : routeDtos) {
                RouteVo routeVo = new RouteVo(routeDto);
                routeVos.add(routeVo);
            }
            result.Success("查询成功",routeVos);
        } catch (Exception e) {
            result.Error("查询失败啦！",e.getMessage());
        }
        return result;
    }

    /**
     * 添加线路
     * @param routeVo 线路信息
     * @return 添加结果
     */
    @PostMapping("/add")
    public ResponseResult<Object> addRoute(@RequestBody RouteVo routeVo) {
        ResponseResult<Object> result = new ResponseResult<>();
        try {
            String voRouteName = routeVo.getRouteName();
            if(routeService.findRouteByName(voRouteName)) {
                result.Error("线路已存在",null);
                return result;
            }
            if(routeVo.getStartPointId() == null
                || routeVo.getEndPointId() == null
                || routeVo.getRouteName() == null
                || routeVo.getRouteName().isEmpty()
                || routeVo.getPoints() == null
                || routeVo.getPoints().isEmpty()
                || routeVo.getTimes() == null
                || routeVo.getTimes().isEmpty()
                ) {
                result.Error("参数出错！请检查",null);
                return result;
            }
            RouteDto routeDto = new RouteDto(routeVo);
            routeService.addRoute(routeDto);
            result.Success("添加成功");
        } catch (Exception e) {
            result.Error("失败",e.getMessage());
        }
        return result;
    }

    /**
     * 删除路线
     * @param id 路线id
     * @return 删除结果
     */
    @DeleteMapping("/delete")
    public ResponseResult<Object> deleteRoute(@RequestParam("id") Integer id) {
        ResponseResult<Object> result = new ResponseResult<>();
        try {
            routeService.deleteRoute(id);
            result.Success("删除成功");
        } catch (Exception e) {
            result.Error("失败",e.getMessage());
        }
        return result;
    }

    /**
     * 更新路线
     * @param routeVo 路线信息
     * @return 更新结果
     */
    @PutMapping("/update")
    public ResponseResult<Object> updateRoute(@RequestBody RouteVo routeVo) {
        ResponseResult<Object> result = new ResponseResult<>();
        try {
            Integer id = routeVo.getId();
            RouteDto dto = routeService.findById(id);
            if(!dto.getRouteName().equals(routeVo.getRouteName())) {
                String voRouteName = routeVo.getRouteName();
                if(routeService.findRouteByName(voRouteName)) {
                    result.Error("线路名称已存在",null);
                    return result;
                }
            }
            RouteDto routeDto = new RouteDto(routeVo);
            routeService.updateRoute(routeDto);
            result.Success("更新成功");
        } catch (Exception e) {
            result.Error("失败",e.getMessage());
        }
        return result;
    }

    /**
     * 根据id查询线路
     * @param routeId 线路id
     * @return 线路信息
     */
    @GetMapping("/findById")
    public ResponseResult<Object> findById(@RequestParam(name = "id") Integer routeId) {
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            RouteDto routeDto= routeService.findById(routeId);
            if (routeDto==null){
                result.BadRequest();
                return result;
            }
            RouteVo routeVo = new RouteVo(routeDto);
            result.Success("查询成功",routeVo);
        }catch (Exception e){
            e.fillInStackTrace();
            result.Error();
        }
        return result;
    }
}
